package cn.ncupyh.soft.staticInnerClass.students.util;

import cn.ncupyh.soft.students.entity.HomeworkRequirement;
import cn.ncupyh.soft.students.entity.HomeworkResult;
import cn.ncupyh.soft.students.entity.Student;
import cn.ncupyh.soft.students.entity.Teacher;

import java.util.ArrayList;
import java.util.Scanner;

public class StuUtil {
    //提交作业
//    c.提交作业 d.查看作业任务 e.查看分数

    public void login(ArrayList<HomeworkRequirement> hwRequirement, ArrayList<HomeworkResult> hwResult
            ,ArrayList<Teacher> teachers){
        //输入学生的信息
        Student stu = new Student();
        //学生登录首页
        //创建学生集合
        ArrayList<Student> students = new ArrayList<>();
        final String submit = "1";
        final String showTask = "2";
        final String forgetPassword = "3";
        final String showGrade = "4";
        final String exit = "0";
        loop:while (true){
            //目录
            System.out.println("---------作业管理系统---------");
            System.out.println("---------学生---登录---------");
            System.out.println("----1>提交作业    2>查看作业任务");
            System.out.println("----3>忘记密码    4>查看分数---");
            System.out.println("----0>退出------------------");
            System.out.println("请输入选项:>");
            Scanner in = new Scanner(System.in);
            String input = in.next();
            //功能实现
            switch(input){
                case exit:{
                    System.out.println("退出!");
                    break loop;
                }
                case submit :{//提交作业
                    submit(stu,teachers,hwResult,hwRequirement);
                    break ;
                }
                case showTask:{//查看任务公告
                    showTask(hwRequirement);
                    break ;
                }
                case forgetPassword :{//忘记密码
                    forgetPassword(students);
                    break ;
                }
                case showGrade :{//查看分数
                    showGrade(hwResult,stu);
                }
                default:{
                    System.out.println(" 输入错误,请重新输入!");
                    break ;
                }
            }
        }
    }

    //查看作业任务公告
    private void showTask(ArrayList<HomeworkRequirement> hwRequirement) {
        System.out.println("科目为：\t作业要求为:\t\t截止日期为:");
        for (int i = 0; i < hwRequirement.size(); i++) {
            System.out.println(hwRequirement.get(i).getSubject() + "\t"+
                    hwRequirement.get(i).getRestrict()  + "\t\t"+
                    hwRequirement.get(i).getDeadline());
        }
    }

    //查看分数
    private void showGrade(ArrayList<HomeworkResult> hwResults,Student stu) {
        //查找答案列表里是否有学生的学号
        int resultIndex = findStuID(hwResults, stu);
        if(resultIndex == -1){
            System.out.println("已提交的作业列表无该学生!");
        }else {
            System.out.println("学号为" + stu.getID() + "的学生" + hwResults.get(resultIndex).getSubject()
                    + "科目的成绩为:" + hwResults.get(resultIndex).getGrade());
        }
    }

    //查找答案列表里是否有学生的学号
    private int findStuID(ArrayList<HomeworkResult> hwResults,Student stu) {
        for (int i = 0; i < hwResults.size(); i++) {
            if(hwResults.get(i).getuID().equals(stu.getID())){
                //有此ID则返回下标
                return i;
            }
        }
        return -1;
    }

    //提交作业
    public void submit(Student stu,ArrayList<Teacher> teachers,ArrayList<HomeworkResult> hwResults
            ,ArrayList<HomeworkRequirement> hwRequirement){
        Scanner in = new Scanner(System.in);
        System.out.println("请输入提交作业的课程的教师名称:>");
        String teaName = in.next();
        //查找是否有这个老师
        int teacherIndex = findteacher(teaName, teachers);
        if(teacherIndex == -1){
            System.out.println("没有"+ teaName +"老师的姓名！");
        }else{
            //显示作业要求
            showSubjectRequirement(teachers.get(teacherIndex),hwRequirement);
        }
        //查找作业答案里面，学生是否已经提交
        boolean b = findStuResult(hwResults, stu, teachers.get(teacherIndex).getSubjects());
        //未提交则提交答案
        if(!b){
            //输入答案
            System.out.println("请输入您的答案:>");
            String stuResult = in.next();
            hwResults.get(hwResults.size()).setuID(stu.getID());//录入学生的学号
            hwResults.get(hwResults.size()).setSubject(teachers.get(teacherIndex).getSubjects());//录入科目
            hwResults.get(hwResults.size()).setResult(stuResult);//录入学生的答案
        }
    }

    //查找作业答案里面，学生是否已经提交
    private boolean findStuResult(ArrayList<HomeworkResult> hwResults,Student stu,String subject) {
        for (int i = 0; i < hwResults.size(); i++) {
            //查找该学生是否已经提交该科目的作业
            if(hwResults.get(i).getuID().equals(stu.getID())){
                if(hwResults.get(i).getSubject().equals(subject)){
                    System.out.println("该学生已经提交过答案，不可再次提交！");
                    return true;
                }
            }
        }
        System.out.println("该学生还提交答案~");
        return false;
    }

    //显示作业要求
    private void showSubjectRequirement(Teacher t, ArrayList<HomeworkRequirement> hwRequirement) {
        for (int i = 0;i<hwRequirement.size();i++) {
            TeacherUtil tu = new TeacherUtil();
            int requireIndex = tu.findSameSubject(hwRequirement, t.getSubjects());
            if(requireIndex == -1){
                System.out.println("作业中没有该科目！");
            }else{
                System.out.println(t.getSubjects() + "的作业要求是" + hwRequirement.get(requireIndex).getRestrict());
            }
        }
    }

//
//    private int findSubject(String subject,ArrayList<Teacher> teachers) {
//
//    }

    //查找是否有这个老师
    private int findteacher(String teaName, ArrayList<Teacher> teachers) {
        for (int i = 0; i < teachers.size(); i++){
            Teacher t = new Teacher();
            if(t.getName().equals(teaName)){
                return i;
            }
        }
        return  -1;
    }

    //忘记密码
    private static void forgetPassword(ArrayList<Student> students) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入用户的ID(账号):");
        String count = in.next();
        //若存在ID,则返回ID索引,否则-1;
        int indexCount = haveID(students, count);
        if (indexCount == -1) {
            System.out.println("查无此用户!未注册!");
            return;
        }
        //继续 找回密码
        System.out.println("请输入身份证:");
        String ID = in.next();
        if(students.get(indexCount).getID().equals(ID)){
            System.out.println("请输入手机号:");
            String phoneNum = in.next();
            if(students.get(indexCount).getPhoneNum().equals(phoneNum)){
                System.out.println("请输入修改后的密码:");
                String password1 = in.next();
                students.get(indexCount).setPassword(password1);
                System.out.println("修改成功!");
                return;
            }else{
                System.out.println("账号信息不匹配，修改失败。");
                return;
            }
        }else{
            System.out.println("账号信息不匹配，修改失败。");
            return;
        }
    }

    //索引学生集合里面是否有这个账号
    private static int haveID(ArrayList<Student> students,String ID) {
        for (int i = 0;i < students.size();i++) {
            Student u = students.get(i);
            String uID = u.getID();
            if(uID.equals(uID)){
                //相等返回索引
                return i;
            }
        }
        return -1;
    }
}

